/**
 * Created by yihua on 2017/3/28.
 */
$(document).ready(function () {
    $(':checkbox[name=flag]').each(function () {
        $(this).click(function () {
            if ($(this).is(':checked')) {
                $(':checkbox[name=flag]').prop("checked", false);
                $(this).prop('checked', true);
                $('#silence_userid').val($(this).val());
            }
        });
    });

    $('#addpic').on('show.bs.modal', function () {
        if (0 == $(':checkbox[name=flag]:checked').length) {
            toastr.error('请选择一个圈子', '', {positionClass: 'toast-bottom-center'});
            return false;
        }
        $("#add_circle_id").val($(':checkbox[name=flag]:checked').val());
        initImg();
    })

    $('#addpic_confirm').unbind('click').click(function () {
        $.ajax({
            url: '/home/circle/addlogo',
            timeout: 100000,
            type: 'get',
            dataType: 'json',
            data: {
                'cir_id': $('#add_circle_id').val(),
                'logo': $("#add_circle_pic").data('url'),
            },
            success: function (rs) {
                if (rs.errno != 0) {
                    toastr.error(rs.errmsg, '', {positionClass: 'toast-bottom-center'});
                } else {
                    toastr.success(rs.errmsg);
                    window.location.reload();
                }
            },
            error: function () {
                toastr.error('系统错误', '', {positionClass: 'toast-bottom-center'});
            }
        });
    });

    function initImg() {
        $("#add_circle_pic").fileinput({
            uploadUrl: "/common/uploadimage", // server upload action
            uploadAsync: true,
            showPreview: false,
            showUpload: false,
            allowedFileExtensions: ['jpg', 'png', 'gif'],
            maxFileCount: 5,
        }).on('fileuploaded', function (event, data, id, index) {
            var ret = data.response;
            if (0 != ret.errno) {
                toastr.error('上传失败', '', {positionClass: 'toast-bottom-center'});
            }
            //bannerImageUrl = ret.data.url;
            //$("#add_circle_pic").val(ret.data.url)
            $("#add_circle_pic").data('url', ret.data.url);
        }).on("filebatchselected", function (event, files) {
            $("#add_circle_pic").fileinput("upload");
        });
    }
});